﻿using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using UnityEngine.UI;

/**
 * 聊天界面
 */
public class ChatWnd : WindowRoot{
    public Image imgWorld;
    public Image imgGroup;
    public Image imgFriend;
    public InputField chatInput;

    public Text txtChat;

    private int chatType;
    private List<string> chatList = new List<string>();

    protected override void InitWnd(){
        base.InitWnd();

        chatType = 0;

        RefreshUI();
    }
    
    public void AddChatMsg(string name, string chat){
        chatList.Add(Constants.Color(name + ":", TxtColor.Blue) + chat);
        if (chatList.Count > 12){
            chatList.RemoveAt(0);
        }

        if (GetWndState()){
            RefreshUI();
        }
    }
    
    private void RefreshUI(){
        if (chatType == 0){
            //世界聊天
            string chatMsg = "";
            for (int i = 0; i < chatList.Count; i++){
                chatMsg += chatList[i] + "\n";
            }
            SetText(txtChat, chatMsg);
            
            SetSprite(imgWorld, "ResImages/btntype1");
            SetSprite(imgGroup, "ResImages/btntype2");
            SetSprite(imgFriend, "ResImages/btntype2");
        }
        else if (chatType == 1){
            //工会聊天
            SetText(txtChat, "当前没有加入工会");
            SetSprite(imgWorld, "ResImages/btntype2");
            SetSprite(imgGroup, "ResImages/btntype1");
            SetSprite(imgFriend, "ResImages/btntype2");
        }
        else if (chatType == 2){
            //好友聊天
            SetText(txtChat, "当前还没有好友");
            SetSprite(imgWorld, "ResImages/btntype2");
            SetSprite(imgGroup, "ResImages/btntype2");
            SetSprite(imgFriend, "ResImages/btntype1");
        }
    }

    public void ClickWorldBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        chatType = 0;
        RefreshUI();
    }
    
    public void ClickGroupBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        chatType = 1;
        RefreshUI();
    }
    
    public void ClickFriendBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        chatType = 2;
        RefreshUI();
    }
    
    public void ClickCloseBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        chatType = 0;
        SetWndState(false);
    }

    private bool canSend = true;
    
    public void ClickSendBtn(){
        if (!canSend){
            GameRoot.AddTips("聊天消息每5秒钟才能发送一次");
            return;
        }
        if (chatInput.text != null && chatInput.text != "" && chatInput.text != " "){
            if (chatInput.text.Length > 12){
                GameRoot.AddTips("输入字符不能超过12个");    
            }

            GameMsg msg = new GameMsg{
                cmd = (int)CMD.SndChat,
                sndChat = new SndChat{
                    chat = chatInput.text
                }
            };

            chatInput.text = "";
            netSvc.SendMsg(msg);
            canSend = false;

            timerSvc.AddTimerTask((int tid) => {
                canSend = true;
            }, 5000);
        }
        else{
            GameRoot.AddTips("尚未输入聊天信息");
        }
    }
}
